local mozhuan = fk.CreateSkill{
  name = "xiaobai__mozhuan",
}

local U = require "packages.utility.utility"

Fk:loadTranslationTable{
  ["xiaobai__mozhuan"] = "墨撰",
  [":xiaobai__mozhuan"] = "转换技，你可以将X+1张黑色牌或非基本牌当任意基本牌使用或打出（X为跨越的项数）："..
    "①【杀】；②【酒】；③【闪】；④【桃】，若均为黑色非基本牌，结算后你令一名角色获得一张【影】。",

  ["@xiaobai__mozhuan"] = "墨撰",
  ["#xiaobai__mozhuan_default_prompt"] = "墨撰：你可以将X+1张黑色牌或非基本牌当任意基本牌使用或打出（X为跨越的项数）",
  ["#xiaobai__mozhuan_prompt"] = "墨撰：你可以将 %arg 张黑色牌或非基本牌【%arg2】使用",

  ["$xiaobai__mozhuan1"] = "（杀）将观旗而动，乃律天时，制为神军。",
  ["$xiaobai__mozhuan2"] = "（酒）撰此文书，再续前盟之好。",
  ["$xiaobai__mozhuan3"] = "（闪）陛下既受帝祉，当施于子孙。",
  ["$xiaobai__mozhuan4"] = "（桃）作降三条，至汝步步高升。",
}


local default_order = {"slash", "analeptic", "jink", "peach"}

---@param mark string
local getStage = function (mark)
  local m = {"①","②","③","④"}
  local index = table.indexOf(m, mark)
  if index < 1 or index > 4 then return 1 end
  return index
end

---@param mark string
local nextStage = function (mark)
  local m = {"①","②","③","④"}
  local index = table.indexOf(m, mark)
  if index < 1 or index > 4 then return m[2] end
  return m[index % 4 + 1]
end

mozhuan:addEffect("viewas", {
  pattern = "slash,jink,peach,analeptic",
  anim_type = "switch",
  mute = true,
  interaction = function (self, player)
    local trans = player:getViewAsCardNames(mozhuan.name, default_order)
    if #trans > 0 then
      return UI.CardNameBox{
        choices = trans
      }
    end
  end,
  prompt = function (self, player, selected_cards, selected)
    local cards = default_order
    if not self.interaction.data then
      return "#xiaobai__mozhuan_default_prompt"
    end
    local count = math.abs(table.indexOf(default_order, self.interaction.data) - getStage(player:getMark("@xiaobai__mozhuan"))) + 1
    return "#xiaobai__mozhuan_prompt:::"..count..":"..self.interaction.data
  end,
  card_filter = function (self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    if card.type == Card.TypeBasic and card.color == Card.Red then return end
    local count = math.abs(table.indexOf(default_order, self.interaction.data) - getStage(player:getMark("@xiaobai__mozhuan"))) + 1
    if #selected < count then return true end
  end,
  view_as = function (self, player, cards)
    if not self.interaction.data or #cards == 0 then return end
    local count = math.abs(table.indexOf(default_order, self.interaction.data) - getStage(player:getMark("@xiaobai__mozhuan"))) + 1
    if #cards < count then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = mozhuan.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    room:setPlayerMark(player, "@xiaobai__mozhuan", nextStage(player:getMark("@xiaobai__mozhuan")))
    if use.card.trueName == "slash" then
      room:notifySkillInvoked(player, mozhuan.name, "offensive")
      player:broadcastSkillInvoke(mozhuan.name, 1)
    elseif use.card.trueName == "jink" then
      room:notifySkillInvoked(player, mozhuan.name, "defensive")
      player:broadcastSkillInvoke(mozhuan.name, 3)
    elseif use.card.trueName == "peach" then
      room:notifySkillInvoked(player, mozhuan.name, "support")
      player:broadcastSkillInvoke(mozhuan.name, 4)
    else
      room:notifySkillInvoked(player, mozhuan.name, "support")
      player:broadcastSkillInvoke(mozhuan.name, 2)
    end
  end,
  after_use = function (self, player, use)
    if player.dead then return end
    for _, cid in ipairs(use.card.subcards) do
      local c = Fk:getCardById(cid)
      if c.type == Card.TypeBasic or c.color ~= Card.Black then return end
    end
    local room = player.room
    local targets = room:getAlivePlayers()
    local to = room:askToChoosePlayers(player, {
      skill_name = mozhuan.name,
      min_num = 1,
      max_num = 1,
      targets = targets,
      cancelable = false
    })
    local shade = U.getShade(room, 1)
    room:obtainCard(to[1], shade, true, fk.ReasonPrey, player, mozhuan.name)
  end
})

return mozhuan
